<script setup lang="ts">
import APIDetails from '@/api/details'
import type { GoodsInfoResult } from '@/api/details/types'
import type { Props, Emits } from './utils/types'
import { ElMessage, ElLoading } from 'element-plus'

const dialogVisible = shallowRef(false)
const $props = defineProps<Props>()
const $emit = defineEmits<Emits>()
const goodsInfo = inject<Ref<GoodsInfoResult>>('goodsInfo')

watch($props, (newValue) => {
    dialogVisible.value = newValue.modelValue
}, { immediate: true })

watch(dialogVisible, (newValue) => {
    if (newValue) {
        getReward()
        getWallet()
    }
    $emit('update:modelValue', newValue)
})

function onBeforeClose() {
    rewardId.value = undefined
    price.value = '0'
    input.value = ''
    dialogVisible.value = false
}

const rewardId = shallowRef<number>()
const list = ref<any[]>([])

function getReward() {
    APIDetails.getReward().then(({ data }) => {
        list.value = data
    })
}

const innerVisible = shallowRef<boolean>(false)

function submitSetRewardOrder() {
    if (price.value == '0' || !price.value) return ElMessage.error('请选择打赏金额')
    innerVisible.value = true
}
const confirmed = shallowRef<any>()

const walletNumber = shallowRef<number>(0)

function getWallet() {
    APIDetails.getWallet().then(({ data }) => {
        walletNumber.value = data.Wallet
    })
}

function onBeforeinnerClose() {
    confirmed.value = undefined
    innerVisible.value = false
}

const price = shallowRef<string>('0')

function handleV(item: any) {
    rewardId.value = item.reward_id
    const data = list.value.filter((item: any) => item.reward_id == rewardId.value)
    if (data.length != 0) {
        if (data[0].amount) {
            price.value = data[0].amount
        }
    }
}

function submitPay() {
    if (!confirmed.value) return ElMessage.error('请先勾选确认订单信息，并认可《心安元素授权服务协议》及《产品退款规则》')
    const data: any = {
        total_amount: price.value,
        id: goodsInfo?.value.id,
        reward_id: rewardId.value,
        type: goodsInfo?.value.type,
        shop_id: goodsInfo?.value.shop_id
    }
    const loading = ElLoading.service({ lock: true })
    APIDetails.setRewardOrder(data).then(() => {
        ElMessage.success('支付成功')
        onBeforeClose()
    }).finally(() => {
        loading.close()
    })
}

function handleJumpWallet() {
    window.location.href = `${import.meta.env.VITE_APP_CONSOLE_URL}/wallet`
}

const input = shallowRef<string>('')

function handleVerify() {
    rewardId.value = undefined
    price.value = input.value
}

</script>

<template>
    <el-dialog class="give" v-model="dialogVisible" title="打赏金额" width="750px" destroy-on-close
        :before-close="onBeforeClose" :show-close="false">
        <template #header>
            <DialogClose title="打赏金额" @close="onBeforeClose" />
        </template>
        <el-dialog class="upload-blockchain" v-model="innerVisible" title="支付" width="1000" destroy-on-close
            :before-close="onBeforeinnerClose" :show-close="false">
            <template #header>
                <DialogClose title="支付" @close="onBeforeinnerClose" />
            </template>
            <div>
                <div class="upload-blockchain-top">
                    <div class="video">
                        <div v-if="goodsInfo?.type == 'video'">
                            <video v-if="goodsInfo?.sample" controls :src="goodsInfo.sample"></video>
                        </div>
                        <div v-if="goodsInfo?.type != 'video'">
                            <img v-if="goodsInfo?.image" :src="goodsInfo.image" alt="image">
                        </div>
                    </div>
                    <div class="video-info">
                        <div class="video-info-title">
                            产品信息：
                        </div>
                        <div class="video-info-item">
                            <span style="font-family: 'SOURCEHANSANSCN-LIGHT';">名 字：</span>{{ goodsInfo?.title }}
                        </div>
                        <div class="video-info-item">
                            <span style="font-family: 'SOURCEHANSANSCN-LIGHT';">类 型：</span><span v-if="goodsInfo?.type == 'video'">视频素材</span>
                            <span v-if="goodsInfo?.type == 'picture'">图片素材</span>
                            <span v-if="goodsInfo?.type == 'music'">音乐素材</span>
                            <span v-if="goodsInfo?.type == 'font'">字体素材</span>
                        </div>
                        <div class="video-info-item">
                            <span style="font-family: 'SOURCEHANSANSCN-LIGHT';">编 号：</span>{{ goodsInfo?.code }}
                        </div>
                    </div>
                </div>
                <div class="upload-blockchain-money">
                    <div></div>
                    <div>本订单才有余额抵扣（当前账户余额 <span>￥{{ walletNumber }}</span> 元）
                        请保证有足够的余额，余额不够时，请点击“<span @click="handleJumpWallet">充值</span>”来增加余额）</div>
                </div>
                <div class="upload-blockchain-sum">
                    <div>应付金额：</div>
                    <div>￥{{ price }}</div>
                </div>
                <div class="upload-blockchain-affirm">
                    <el-radio :value="true" size="large" v-model="confirmed" text-color="#606266">
                        <div style="color: #606266; font-family: 'SOURCEHANSANSCN-LIGHT';">确认订单信息，并认可<span class="active">《心安元素授权服务协议》</span>及<span
                                class="active">《产品退款规则》</span></div>
                    </el-radio>
                </div>
                <div class="upload-blockchain-btn">
                    <button @click="submitPay">支付</button>
                </div>
            </div>
        </el-dialog>
        <div class="select-money">
            <div class="select-money-item " @click="handleV(item)" v-for="item in list" :key="item.id"
                :class="{ 'select-money-item-active': rewardId == item.reward_id }">
                {{ item.title }}
            </div>
        </div>
        <div class="custom">
            <el-input v-model="input" @input="(v: any) => (input = v.replace(/^(0+)|[^\d]+/g, ''))" placeholder="自定义金额">
                <template #append>
                    <el-button type="primary" @click="handleVerify">确认</el-button>
                </template>
            </el-input>
        </div>
        <template #footer>
            <div>
                <span style="color: #606266;margin-right: 20px;">已选择金额：{{ price }}</span>
                <el-button type="primary" @click="submitSetRewardOrder" size="large">打赏</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<style lang="scss" scoped>
.upload-blockchain {

    .upload-blockchain-btn {
        display: flex;

        button {
            width: 176px;
            height: 50px;
            border-radius: 5px;
            background-color: rgba(249, 20, 20, 1);
            color: rgba(255, 255, 255, 1);
            font-size: 14px;
            text-align: center;
            border: none;
            cursor: pointer;
            margin-left: auto;
            margin-right: 42px;
        }
    }

    .upload-blockchain-affirm {
        display: flex;
        justify-content: flex-end;
        margin-right: 42px;

        .active {
            color: #3894FF;
        }
    }

    .upload-blockchain-sum {
        display: flex;
        justify-content: flex-end;
        align-items: flex-end;
        margin-right: 42px;
        margin-top: 10px;

        div {
            &:nth-child(1) {
                color: rgba(16, 16, 16, 1);
                font-size: 14px;
            }

            &:nth-child(2) {
                color: rgba(249, 20, 20, 1);
                font-size: 36px;
                line-height: 1em;
                font-weight: 700;
            }
        }
    }

    .upload-blockchain-money {
        display: flex;
        justify-content: center;
        gap: 0 16px;
        margin-top: 56px;
        font-family: 'SOURCEHANSANSCN-LIGHT';

        div {
            &:nth-child(1) {
                width: 4px;
                height: 50px;
                background-color: #FF0000;
            }

            &:nth-child(2) {
                width: 860px;
                height: 50px;
                line-height: 50px;
                border-radius: 5px;
                background-color: rgba(238, 238, 239, 1);
                border: 1px dashed rgba(187, 187, 187, 1);
                font-size: 14px;
                color: #3894FF;
                text-align: center;

                span {
                    color: #F91414;
                    cursor: pointer;
                }
            }
        }
    }

    .upload-blockchain-top {
        display: flex;
        gap: 0 44px;

        .video-info {
            color: rgba(16, 16, 16, 1);

            .video-info-title {
                color: rgba(16, 16, 16, 1);
                font-size: 14px;
                display: flex;
                align-items: center;
                line-height: 20px;
                margin-bottom: 10px;
                font-family: 'SOURCEHANSANSCN-REGULAR';

                &::before {
                    content: '';
                    display: block;
                    width: 3px;
                    height: 14px;
                    background-color: #FF0000;
                    margin-right: 8px;
                }
            }

            .video-info-item {
                color: rgba(16, 16, 16, 1);
                line-height: 35px;
                font-size: 14px;
                font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
            }
        }

        .video {
            width: 256px;
            height: 256px;
            background-color: #eeeeee;
            display: flex;
            align-items: center;

            div {
                width: 100%;
                height: 100%;

                video {
                    width: 100%;
                    height: 100%;
                    object-fit: contain;
                }

                img {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                }
            }


        }
    }

}

.give {

    .custom {
        margin-top: 30px;
    }

    .select-money {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        margin-top: 30px;
        gap: 30px;

        div.select-money-item-active {
            background-color: rgba(56, 148, 255, 1);
            color: #ffffff;
        }

        .select-money-item {
            height: 50px;
            line-height: 50px;
            border-radius: 5px;
            font-size: 14px;
            text-align: center;
            color: #888888;
            background-color: rgba(238, 238, 239, 1);
            cursor: pointer;
        }
    }
}
</style>
